// mircoApp/cloud_shop/customersMy/customersMy.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    recent_count: 0,
    usable_count: 0,
    active_nav: "0",
    active: "all",
    page: 1,
    total_page: 0,
    isLoadMore: true,
    dataList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.get_sum_Data();
    this.init();
    this.getData();
  },
  tabChange(e) {
    this.setData({
      active_nav: e.detail.index,
    });
    if (this.data.active != "all") {
      this.setData({
        active: "all",
      });
      this.selectComponent("#childTaba").resize();
      return;
    } else {
      this.init();
      this.getData();
      return;
    }
  },
  childTabchange(e) {
    let name = e.detail.name;
    this.setData({
      active: name,
    });
    this.init();
    this.getData();
  },
  to_customersDetail(e) {
    let {
      id
    } = e.currentTarget.dataset;
    wx.navigateTo({
      url: "/mircoApp/cloud_shop/customersDetail/customersDetail?id=" + id,
    });
  },
  init() {
    this.setData({
      page: 1,
      total_page: 0,
      isLoadMore: true,
      dataList: [],
    });
  },
  async getData() {
    let json = {};
    if (this.data.active_nav == 0) {
      json.is_new = 1;
    } else if (this.data.active_nav == 1){
      json.is_usable = 1;
    }
    if (this.data.active == "is_today") {
      json.is_today = 1;
    } else if (this.data.active == "is_buy") {
      json.is_buy = 1;
    } else if (this.data.active == "is_usable") {
      json.is_usable = 1;
    }
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.cloud-shop.frontend.customer.index"),
      data: json,
    });
    if (data.result) {
      this.setData({
        dataList: data.data.data,
        total_page: data.data.last_page,
        isLoadMore: true,
      });
    } else {
      app.tips(data.msg);
    }
  },
  async getMoreData() {
    if (!this.data.isLoadMore) return;
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.setData({
        page: this.data.page + 1,
      });
      let json = {
        page: this.data.page,
      };
      if (this.data.active_nav == 0) {
        json.is_new = 1;
      } else if (this.data.active_nav == 1){
        json.is_usable = 1;
      }
      if (this.data.active == "is_today") {
        json.is_today = 1;
      } else if (this.data.active == "is_buy") {
        json.is_buy = 1;
      } else if (this.data.active == "is_usable") {
        json.is_usable = 1;
      }
      let {
        data
      } = await app._postNetWork({
        url: app.getNetAddresss("plugin.cloud-shop.frontend.customer.index"),
        data: json,
      });
      this.data.isLoadMore = true;
      if (data.result) {
        this.setData({
          dataList: this.data.dataList.concat(data.data.data),
        });
      } else {
        this.setData({
          page: this.data.page - 1,
        });
        app.tips(data.msg);
      }
    }
  },
  async get_sum_Data() {
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.cloud-shop.frontend.customer.summary"),
    });
    if (data.result) {
      this.setData({
        recent_count: data.data.recent_count,
        usable_count: data.data.usable_count,
      });
    } else {
      app.tips(data.msg);
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});